/**
 * @param {string} s
 * @param {string} p
 * @return {number[]}
 */
 var findAnagrams = function(s, p) {
  // let map = new Map()
  let plen = p.length
  let res = []
  let map = new Map()
  for(let item of p){
    map.set(item,map.get(item)?map.get(item)+1:1)
  }
  let window = new Map()
  let valid = 0
  for(let i = 0;i < s.length;i++){
    let right = s[i]
    window.set(right,window.get(right)? window.get(right)+1 : 1)
    if(window.get(right) === map.get(right)){
      valid++
    }
    if(i >= plen){
      let left = s[i-plen]
      if(window.get(left) === map.get(left)){
        valid--
      }
      window.set(left,window.get(left)-1)
    }
    if(valid === map.size){
      res.push(i-plen+1)
    }
  }
  return res
};